(function (vc) {
    vc.extends({
        data: {
            editOwnerMemberInfo: {
                ownerId: '',
                memberId: '',
                ownerTypeCd: '',
                name: '',
                age: '',
                link: '',
                address: '',
                sex: '',
                remark: '',
                ownerPhoto: '',
                ownerPhotoUrl: '',
                idCard: '',
                card: '',
                flag: '',
                personType:'P',
                personRole:'1',
                concactPerson:'',
                concactLink:'',
                attrs: []
            }
        },
        _initMethod: function () {
            $that._loadEditOwnerMemberAttrSpec();
        },
        _initEvent: function () {
            vc.on('editOwnerMember', 'openEditOwnerMemberModal', function (_owner) {
                //清理 上次数据
                $that.clearEditOwnerMemberInfo();
                vc.copyObject(_owner, $that.editOwnerMemberInfo);
                //根据memberId 查询 照片信息
                $that.editOwnerMemberInfo.ownerPhoto = _owner.faceUrl;
                $that.editOwnerMemberInfo.ownerPhotoUrl = _owner.faceUrl;
                $('#editOwnerMemberModel').modal('show');
                if (_owner.hasOwnProperty('ownerAttrDtos')) {
                    let _ownerAttrDtos = _owner.ownerAttrDtos;
                    _ownerAttrDtos.forEach(item => {
                        $that.editOwnerMemberInfo.attrs.forEach(attrItem => {
                            if (item.specCd == attrItem.specCd) {
                                attrItem.attrId = item.attrId;
                                attrItem.value = item.value;
                            }
                        })
                    })
                }
            });
        },
        methods: {
            editOwnerMemberValidate: function () {
                return vc.validate.validate({
                    editOwnerMemberInfo: $that.editOwnerMemberInfo
                }, {
                    'editOwnerMemberInfo.name': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "名称不能为空"
                        },
                        {
                            limit: "maxin",
                            param: "2,64",
                            errInfo: "名称长度必须在2位至64位"
                        }
                    ],
                    'editOwnerMemberInfo.sex': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "性别不能为空"
                        }
                    ],
                    'editOwnerMemberInfo.link': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "手机号不能为空"
                        },
                        {
                            limit: "phone",
                            param: "",
                            errInfo: "手机号格式错误"
                        }
                    ],
                    'editOwnerMemberInfo.remark': [
                        {
                            limit: "maxLength",
                            param: "200",
                            errInfo: "备注长度不能超过200位"
                        }
                    ]
                });
            },
            editOwnerMemberMethod: function () {
                if (!$that.editOwnerMemberValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                $that.editOwnerMemberInfo.communityId = vc.getCurrentCommunity().communityId;
                vc.http.apiPost(
                    '/owner.editOwnerMember',
                    JSON.stringify($that.editOwnerMemberInfo), {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#editOwnerMemberModel').modal('hide');
                            vc.emit('ownerDetailMember', 'listOwnerData', $that.editOwnerMemberInfo);
                            vc.emit('simplifyOwnerMember', 'listOwnerData', $that.editOwnerMemberInfo);

                            vc.toast("修改成功");
                            return;
                        } else {
                            vc.toast(_json.msg);
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            clearEditOwnerMemberInfo: function () {
                let _attrs = $that.editOwnerMemberInfo.attrs;
                _attrs.forEach(item => {
                    item.value = '';
                })
                $that.editOwnerMemberInfo = {
                    ownerId: '',
                    memberId: '',
                    ownerTypeCd: '',
                    name: '',
                    age: '',
                    link: '',
                    address: '',
                    sex: '',
                    remark: '',
                    ownerPhoto: '',
                    ownerPhotoUrl: '',
                    idCard: '',
                    personType:'P',
                    personRole:'1',
                    concactPerson:'',
                    concactLink:'',
                    attrs: _attrs
                };
            },
            _uploadEditMemberPhoto: function (event) {
                $("#uploadEditOwnerMemberPhoto").trigger("click")
            },
            _chooseEditMemberPhoto: function (event) {
                let photoFiles = event.target.files;
                if (photoFiles && photoFiles.length > 0) {
                    // 获取目前上传的文件
                    var file = photoFiles[0]; // 文件大小校验的动作
                    if (file.size > 1024 * 1024 * 1) {
                        vc.toast("图片大小不能超过 2MB!")
                        return false;
                    }
                    // 改为异步上传图片
                    this._doUploadImageEditOwnerMember(file);
                }
            },
            // 异步上传图片
            _doUploadImageEditOwnerMember: function (_file) {
                var param = new FormData();
                param.append("uploadFile", _file);
                param.append('communityId', vc.getCurrentCommunity().communityId);
                //发送get请求
                vc.http.upload('uploadFile',
                    'uploadImage',
                    param, {
                        emulateJSON: true,
                        //添加请求头
                        headers: {
                            "Content-Type": "multipart/form-data"
                        }
                    },
                    function (json, res) {
                        if (res.status != 200) {
                            vc.toast("上传文件失败");
                            return;
                        }
                        let data = JSON.parse(json);
                        $that.editOwnerMemberInfo.ownerPhoto = data.fileId;
                        $that.editOwnerMemberInfo.ownerPhotoUrl = data.url;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    }
                );
            },
            _loadEditOwnerMemberAttrSpec: function () {
                $that.editOwnerMemberInfo.attrs = [];
                vc.getAttrSpec('building_owner_attr', function (data) {
                    data.forEach(item => {
                        item.value = '';
                        item.values = [];
                        $that._loadEditMemberAttrValue(item.specCd, item.values);
                        if (item.specShow == 'Y') {
                            $that.editOwnerMemberInfo.attrs.push(item);
                        }
                    });
                });
            },
            _loadEditMemberAttrValue: function (_specCd, _values) {
                vc.getAttrValue(_specCd, function (data) {
                    data.forEach(item => {
                        if (item.valueShow == 'Y') {
                            _values.push(item);
                        }
                    });
                });
            },
            _closeEditOwnerMemberModal: function () {
              
                $('#editOwnerMemberModel').modal('hide');
            },
        }
    });
})(window.vc);